/**
 * Copyright (c) Honor Device Co., Ltd. 2021. All rights reserved.
 */

package com.wangfugui.mybatisplus.common.utils;


import lombok.extern.slf4j.Slf4j;
import javax.xml.bind.DatatypeConverter;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * ID生成工具
 *
 * @author: nathanye
 * @since: 2021/10/25 18:48
 */
//@Component
@Slf4j
public class IdUtils {

//    private static RedisTemplate redisTemplate;
//
////    @Autowired(required = false)
//    public void setRedisTemplate(RedisTemplate redisTemplate) {
//        redisStatic(redisTemplate);
//    }
//
//    private static void redisStatic(RedisTemplate redisTemplate) {
//        IdUtils.redisTemplate = redisTemplate;
//    }

    private static final String REDIS_PREFIX_SNOWFLAKE = "snowflake-";

    private static final Integer DATA_SIZE = 1024;

    private static final String DEFAULT_APP_NAME = "default";




    /**
     * 生成递增ID
     *
     * @return
     */
    public static String getId() {
        return String.valueOf(UUID.randomUUID());
    }

    /**
     * 单机生成唯一ID
     *
     * @return
     */
    public static String genUuid() {
        return UUID.randomUUID().toString();
    }


    private static final Map<String, Map<String, Integer>> WORKER_AND_DATA_CENTER_ID_MAP = new ConcurrentHashMap<>();





    private static Map<String, Integer> getDefaultIdMap() {
        Map<String, Integer> ids = new HashMap<>();
        ids.put("workerId", 1);
        ids.put("datacenterId", 1);
        return ids;
    }

    /**
     * 获取uuid
     *
     * @return DatatypeConverter
     * @throws NoSuchAlgorithmException
     */
    public static String generateRandomUuid() throws NoSuchAlgorithmException {
        SecureRandom secureRandom = SecureRandom.getInstanceStrong();
        byte[] bytes = new byte[24];
        secureRandom.nextBytes(bytes);
        return DatatypeConverter.printHexBinary(bytes).toLowerCase(Locale.ROOT);
    }
}
